<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.faf.bean.Food"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() + path+"/"+"UserGlobal"+"/";
%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>饭否-详情</title>
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/reset.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/main.css">
	<script type="text/javascript" src="<%=basePath%>js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/jquery-ui.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/slide.js"></script>
</head>
<% 
	Object pre_global_info_user_name = request.getSession().getAttribute("account");
	String global_info_user_name;
	HashMap<Integer, Integer> global_info_cart_id2count = null;
	int i_food_count = 0;
	
	if(null != pre_global_info_user_name){
		global_info_user_name = pre_global_info_user_name.toString();
		
		global_info_cart_id2count = (HashMap<Integer, Integer>)request.getSession().getAttribute("global_cart");
		for(int value : global_info_cart_id2count.values())
			i_food_count += value;
		
	} else {
		global_info_user_name = "";
	}
	
	request.getSession().setAttribute("pre-page", "/FAFCode/Detail" + "?" + request.getQueryString());
	
	Object obj_food = request.getAttribute("detail_data_food");
	Food food = (Food)obj_food;
%>
<body>
	<lable id="foodid_" hidden=true><%=food.getFoodId() %></lable>
	<lable id="userlg_" hidden=true><%=null == pre_global_info_user_name ? 0 : 1%></lable>
	<lable id="foodlike_count_" hidden=true><%=(long)request.getAttribute("likecount") %></lable>
	<lable id="foodlike_isme_" hidden=true><%=(boolean)request.getAttribute("likeisme") ? 1 : 0 %></lable>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到饭否!</div>
			<div class="fr">
				<div class="login_info fl">
					欢迎您：<em><%= null == pre_global_info_user_name ? global_info_user_name : "请登录" %></em>
				</div>
				<% if(null == pre_global_info_user_name) { %>
				<div class="login_btn fl">
					<a href="/FAFCode/UserManagement/login.jsp">登录</a>
					<span>|</span>
					<a href="/FAFCode/UserManagement/register.jsp">注册</a>
				</div>
				<% } else { %>
				<div class="user_link fl">
					<a href="/FAFCode/UserManagement/user_center_info.jsp">用户中心</a>
					<span>|</span>
					<a href="/FAFCode/MyCart">我的购物车</a>
					<span>|</span>
					<a href="/FAFCode/UserManagement/user_center_order.jsp">我的订单</a>
				</div>
				<% } %>
			</div>
		</div>		
	</div>
	
	<div class="search_bar clearfix">
		<a href="Index" class="logo fl"><img src="/FAFCode/Image/logo/logo.png"></a>
		<form action="List" >
		<div class="search_con fl">
			<input type="text" class="input_text fl" name="serchContect" placeholder="搜索商品">
			<input type="submit" class="input_btn fr" name="" value="搜索">
		</div>
		</form>
		<div class="guest_cart fr">
			<a href="/FAFCode/MyCart" class="cart_name fl">我的购物车</a>
			<div class="goods_count fl" id="show_count"><%=i_food_count %></div>
		</div>
	</div>
	
	<div class="navbar_con">
		<div class="navbar clearfix">
			<div class="subnav_con fl">
				<h1>全部菜品分类</h1>	
				<span></span>			
				<ul class="subnav">
					<li><a href="List?flag=1" class="fruit">炒菜</a></li>
					<li><a href="List?flag=2" class="seafood">主食</a></li>
					<li><a href="List?flag=3" class="meet">凉菜</a></li>
					<li><a href="List?flag=4" class="egg">饮品</a></li>
					<li><a href="List?flag=5" class="vegetables">速食</a></li>
					<li><a href="List?flag=6" class="ice">水果</a></li>					
				</ul>
			</div>
			<ul class="navlist fl">
				<li><a href="Index">首页</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=1">炒菜</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=2">主食</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=3">凉菜</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=4">饮品</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=5">速食</a></li>
				<li class="interval">|</li>
				<li><a href="List?flag=6">水果</a></li>
			</ul>
		</div>
	</div>
	<div class="goods_detail_con clearfix">
		<div class="goods_detail_pic fl"><img width=350 height=350 src="<%= "/FAFCode/"+food.getFoodPhoto() %>"></div>

		<div class="goods_detail_list fr">
			<h3><%= food.getFoodName() %></h3>
			<p><%= food.getFoodDescribe() %></p>
			<div class="prize_bar">
				<span class="show_pirze">¥<em id="spm"><%= food.getFoodPrice() %></em></span>
				<span class="show_unit">单  位：1份</span>
			</div>
			<div class="goods_num clearfix">
				<div class="num_name fl">数 量：</div>
				<div class="num_add fl">
					<input type="text" class="num_show fl" id="num_" value="1">
					<a href="javascript:;" class="add fr" id="up">+</a>
					<a href="javascript:;" class="minus fr" id="down">-</a>	
				</div> 
			</div>
			<div class="total">总价：<em id="tMoney"><%= food.getFoodPrice() %></em></div>
			<div class="operate_btn">
				<a href="javascript:;" class="buy_btn" id="buy_now">立即购买</a>
				<a href="javascript:;" class="add_cart" id="add_cart">加入购物车</a>
				<% if(null == pre_global_info_user_name){ %>
				<a href="/FAFCode/UserManagement/login.jsp" class="like_0" id="like_0"><%="喜欢("+(long)request.getAttribute("likecount")+")"%></a>
				<% } else if((boolean)request.getAttribute("likeisme")) { %>
				<a href=<%="/FAFCode/LikeSetter?fid="+food.getFoodId()+"&action=0" %> class="like_1" id="like_1"><%="喜欢("+(long)request.getAttribute("likecount")+")"%></a>
				<% } else {%>
				<a href=<%="/FAFCode/LikeSetter?fid="+food.getFoodId()+"&action=1" %> class="like_0" id="like_0"><%="喜欢("+(long)request.getAttribute("likecount")+")"%></a>
				<% } %>
			</div>
		</div>
	</div>
	<script type="text/javascript" src="js/jquery-1.12.2.js"></script>
	<script type="text/javascript">

		$('#add_cart').click(function(){
			var isLogin = document.getElementById('userlg_').innerText;
			if(0 == isLogin){
				window.location.href="/FAFCode/UserManagement/login.jsp";
				return;
			}
			var food_count = document.getElementById('num_').value;
			var food_id = document.getElementById('foodid_').innerText;
			window.location.href= "/FAFCode/AddCart?id=" + food_id + "&num=" + food_count;
		})
		
		var count_num = 1;
		
		$('#up').click(function(){
			if(count_num >= 99)
				return;
			document.getElementById('num_').value= ++count_num;
			updateTotalPrice();
		})
		
		$('#down').click(function(){
			if(count_num <= 1)
				return;
			document.getElementById('num_').value = --count_num;
			updateTotalPrice();
		})
		
		$('#buy_now').click(function(){
			var isLogin = document.getElementById('userlg_').innerText;
			if(0 == isLogin){
				window.location.href="/FAFCode/UserManagement/login.jsp";
				return;
			}
			var food_count = document.getElementById('num_').value;
			var food_id = document.getElementById('foodid_').innerText;
			window.location.href= "/FAFCode/BuyNow?id=" + food_id + "&num=" + food_count;
		})
		
		function updateTotalPrice(){
			var m = document.getElementById('spm').innerText;
			document.getElementById('tMoney').innerText= numMulti(m, count_num) + "元";
		}
		
        function numMulti(num1, num2) {
            var baseNum = 0;
            try {
                baseNum += num1.toString().split(".")[1].length;
            } catch (e) {
            }
            try {
                baseNum += num2.toString().split(".")[1].length;
            } catch (e) {
            }
            return Number(num1.toString().replace(".", ""))
                    * Number(num2.toString().replace(".", ""))
                    / Math.pow(10, baseNum);
        };
		
	</script>
</body>
</html>